package com.youi.service.impl;

import com.youi.common.Common;
import com.youi.common.DelStatus;
import com.youi.common.OrderStatusEnum;
import com.youi.entity.Goods;
import com.youi.entity.Goodsandgoodscar;
import com.youi.entity.Goodscar;
import com.youi.entity.Goodsimgs;
import com.youi.entity.Goodssize;
import com.youi.entity.Merchant;
import com.youi.entity.Ordergoods;
import com.youi.entity.User;
import com.youi.entity.Useraddress;
import com.youi.entity.Userorder;
import com.youi.entity.Userwechat;
import com.youi.entity.Yibaopayment;
import com.youi.exception.BusinessException;
import com.youi.service.GoodsService;
import com.youi.service.GoodsandgoodscarService;
import com.youi.service.GoodscarService;
import com.youi.service.GoodsimgsService;
import com.youi.service.GoodssizeService;
import com.youi.service.MerchantService;
import com.youi.service.OrdergoodsService;
import com.youi.service.UserorderService;
import com.youi.service.YibaopaymentService;
import com.youi.util.CommonInfoUtil;
import com.youi.util.JsonUtil;
import com.youi.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TransactionalUserorderService {

    private static HashMap<String, Object> params(String key, Object value) {
        HashMap<String, Object> result = new HashMap<>();
        result.put(key, value);
        return result;
    }

    private static HashMap<String, Object> paramsNODel(String key, Object value) {
        HashMap<String, Object> result = paramsNODel();
        result.put(key, value);
        return result;
    }

    private static HashMap<String, Object> paramsNODel() {
        HashMap<String, Object> result = new HashMap<>();
        result.put(Common.DELSTATUS, DelStatus.NODEL);
        return result;
    }

    static private Comparator<Long> longComparator = new Comparator<Long>() {
        @Override
        public int compare(Long o1, Long o2) {
            return Long.compare(o1, o2);
        }
    };

    @Autowired
    private UserorderService userorderService;
    @Autowired
    private MerchantService merchantService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private OrdergoodsService ordergoodsService;
    @Autowired
    private GoodscarService goodscarService;
    @Autowired
    private GoodsandgoodscarService goodsandgoodscarService;
    @Autowired
    private GoodssizeService goodssizeService;
    @Autowired
    private GoodsimgsService goodsimgsService;
    @Autowired
    private YibaopaymentService yibaopaymentService;

//    //自我注入，据说spring4.3版本之后已经支持
//    @Autowired
//    private TransactionalUserorderService selfService;

    //数据结构，货品和型号ID，以及数量，或者购物车明细Entity
    static private class GoodssizeAndCount {
        Long goodssizeId;
        Long goodsId;
        Integer singleCount;
        Goodsandgoodscar goodsandgoodscar;

        GoodssizeAndCount(Long goodssizeId, Long goodsId, Integer singleCount) {
            this.goodssizeId = goodssizeId;
            this.goodsId = goodsId;
            this.singleCount = singleCount;
            this.goodsandgoodscar = null;
        }

        GoodssizeAndCount(Long goodssizeId, Long goodsId, Goodsandgoodscar goodsandgoodscar) {
            this.goodssizeId = goodssizeId;
            this.goodsId = goodsId;
            this.singleCount = null;
            this.goodsandgoodscar = goodsandgoodscar;
        }
    }

    //数据结构，店铺和在该店铺内的购物信息（货品和型号ID）
    static private class MerchantAndGoods {
        Merchant merchant;
        ArrayList<GoodssizeAndCount> goodssizeAndCountList = new ArrayList<>();

        MerchantAndGoods(Merchant merchant) {
            this.merchant = merchant;
        }
    }

    //加锁帮助类，可以对提供的货品和型号ID集合进行排序和加锁，并保留加锁后加载的对象
    private class LockedGoodsAndGoodssize {
        HashMap<Long, Goods> goodsMap = new HashMap<>();
        HashMap<Long, Goodssize> goodssizeMap = new HashMap<>();

        LockedGoodsAndGoodssize(HashSet<Long> allGoodsIdSet, HashSet<Long> allGoodssizeIdSet) {
            ArrayList<Long> allGoodsIds = new ArrayList<>(allGoodsIdSet);
            ArrayList<Long> allGoodssizeIds = new ArrayList<>(allGoodssizeIdSet);
            allGoodsIds.sort(longComparator);
            allGoodssizeIds.sort(longComparator);
            //排序完成后的数据进行加锁获取记录,顺序为先goods，然后是goodssize
            for (Long goodsId : allGoodsIds) {
                Goods goods = goodsService.getForUpdate(goodsId);
                goodsMap.put(goods.getId(), goods);
            }
            for (Long _goodssizeId : allGoodssizeIds) {
                Goodssize goodssize = goodssizeService.getForUpdate(_goodssizeId);
                goodssizeMap.put(goodssize.getId(), goodssize);
            }
        }
    }


    /**
     * 提交订单的事务过程
     *
     * @param goodssizeId         1. 单一货品提交
     * @param singleCount         1. 单一货品提交数量
     * @param goodsandgoodscarIds 2. 购物车多个货品提交
     * @param user                其余必要参数：用户
     * @param userwechat          其余必要参数：微信用户
     * @param useraddress         其余必要参数：用户收货地址
     * @param leaderId            其余必要参数：团长id
     * @param orderType           其余必要参数：订单类型
     * @param remark              其余必要参数：订单备注
     * @return
     * @throws BusinessException
     */
    @Transactional
    public String submitUserOrder(Long goodssizeId, Integer singleCount, String goodsandgoodscarIds,
                                  User user, Userwechat userwechat, Useraddress useraddress, Long leaderId,
                                  Integer orderType, String remark) throws BusinessException {
        //整理好的所有店铺信息以及下带购物车信息
        ArrayList<MerchantAndGoods> allMerchantList = new ArrayList<>();
        //从购物车购买时，处理完成后要清空合计
        Goodscar goodscar = null;
        //根据商户拆分订单
        List<Userorder> orderList = new ArrayList<>();

        //填充 allMerchantList 内容
        if ((goodssizeId != null) && (singleCount != null)) {
            //如果是单个购买
            Goodssize goodssize = goodssizeService.getByParam(paramsNODel(Common.ID, goodssizeId));
            if (goodssize == null) {
                return JsonUtil.toJson(CommonInfoUtil.NULLOBJECT, "商品规格不存在");
            }
            Goods goods = goodsService.getByParam(paramsNODel(Common.ID, goodssize.getGoodsId()));
            if (goods == null) {
                return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "商品不存在");
            }
            Merchant merchant = merchantService.getByParam(paramsNODel(Common.ID, goods.getMerchantId()));
            if (merchant == null) {
                return JsonUtil.toJson(CommonInfoUtil.NULLOBJECT, "商家信息不存在");
            }

            MerchantAndGoods merchantAndGoods = new MerchantAndGoods(merchant);
            GoodssizeAndCount goodssizeAndCount = new GoodssizeAndCount(goodssize.getId(), goods.getId(), singleCount);
            merchantAndGoods.goodssizeAndCountList.add(goodssizeAndCount);
            allMerchantList.add(merchantAndGoods);

        } else if (goodsandgoodscarIds != null) {
            //从购物车购买
            goodscar = goodscarService.getByParam(paramsNODel(Common.USERID, user.getId()));
            if (goodscar == null) {
                return JsonUtil.toJson(CommonInfoUtil.NULLOBJECT, "无法获取goodscar");
            }
            List<Long> idsList = ObjectUtil.getIdListByIds(goodsandgoodscarIds);
            if (idsList == null || idsList.size() == 0) {
                return JsonUtil.toJson(CommonInfoUtil.PARAMERROR, "购物车商品信息id错误");
            }
            List<Goodsandgoodscar> goodsandgoodscarList = goodsandgoodscarService.getByParams(paramsNODel(Common.ID, idsList));
            if (CollectionUtils.isEmpty(goodsandgoodscarList)) {
                return null;
            }
            //将数据分散到allMerchantList的各个MerchantAndGoods记录里面去
            for (Goodsandgoodscar goodsandgoodscar : goodsandgoodscarList) {
                Goods goods = goodsService.getByParam(paramsNODel(Common.ID, goodsandgoodscar.getGoodsId()));
                if (goods != null) {
                    ArrayList<GoodssizeAndCount> goodssizeAndCountList = null;
                    //在allMerchantList找符合的merchant
                    for (MerchantAndGoods merchantAndGoods : allMerchantList) {
                        if (merchantAndGoods.merchant.getId().equals(goods.getMerchantId())) {
                            goodssizeAndCountList = merchantAndGoods.goodssizeAndCountList;
                            break;
                        }
                    }
                    //如果对应的MerchantAndGoods还没添加，就新增一个
                    if (goodssizeAndCountList == null) {
                        Merchant merchant = merchantService.getByParam(paramsNODel(Common.ID, goods.getMerchantId()));
                        if (merchant == null) {
                            return JsonUtil.toJson(CommonInfoUtil.NULLOBJECT, "商家信息不存在");
                        }
                        MerchantAndGoods merchantAndGoods = new MerchantAndGoods(merchant);
                        allMerchantList.add(merchantAndGoods);
                        goodssizeAndCountList = merchantAndGoods.goodssizeAndCountList;
                    }
                    GoodssizeAndCount goodssizeAndCount = new GoodssizeAndCount(
                            goodsandgoodscar.getGoodssizeId(), goods.getId(), goodsandgoodscar);
                    goodssizeAndCountList.add(goodssizeAndCount);
                }
            }
        } else {
            return JsonUtil.toJson(CommonInfoUtil.PARAMERROR, "必须提供需要提交的订单参数");
        }

        if (allMerchantList.isEmpty()) {
            return JsonUtil.toJson(CommonInfoUtil.NULLOBJECT, "没有数据");
        }

        //至此 allMerchantList 已经准备就绪，接下来进行排序加锁，为了预防死锁必须先排序
        HashSet<Long> allGoodsIdSet = new HashSet<>();
        HashSet<Long> allGoodssizeIdSet = new HashSet<>();
        //先采集所有的goodsId和goodssizeId
        for (MerchantAndGoods merchantAndGoods : allMerchantList) {
            for (GoodssizeAndCount goodssizeAndCount : merchantAndGoods.goodssizeAndCountList) {
                allGoodsIdSet.add(goodssizeAndCount.goodsId);
                allGoodssizeIdSet.add(goodssizeAndCount.goodssizeId);
            }
        }
        //排序，锁定和加载
        LockedGoodsAndGoodssize lockedData = new LockedGoodsAndGoodssize(allGoodsIdSet, allGoodssizeIdSet);

        //加锁完成后，任何业务逻辑失败都要回滚已经保存的数据，因此后面都用抛出例外的办法来处理
        double payAllAmount = 0.0;

        //对于每个merchant循环
        for (MerchantAndGoods merchantAndGoods : allMerchantList) {

            Merchant merchant = merchantAndGoods.merchant;

//            double amount=0.00;
//            double payableAmount=0.00;
            //一个店铺下所有金额合计
            double goodsAnt = 0.00;

            Userorder userorder = userorderService.addUserorder(user, useraddress, merchant, orderType);
            if (leaderId != null) {
                userorder.setLeaderId(leaderId);
            } else {
                userorder.setLeaderId(0L);
            }
            userorder.setUnioid(userwechat.getUnionid());

            for (GoodssizeAndCount goodssizeAndCount : merchantAndGoods.goodssizeAndCountList) {
                Goods goods = lockedData.goodsMap.get(goodssizeAndCount.goodsId);
                if (goods == null) {
                    throw new BusinessException(CommonInfoUtil.NULLOBJECT, "商品信息不存在");
                }
                if (goods.getStatus() != 1) {
                    throw new BusinessException(CommonInfoUtil.NOPERMISSIONS, "商品非出售状态无法出售");
                }
//                goodsService.getGoodsMsg(goods, user); 尽量不调用这个方法完成功能

                Goodssize goodssize = lockedData.goodssizeMap.get(goodssizeAndCount.goodssizeId);
                if (goodssize == null) {
                    throw new BusinessException(CommonInfoUtil.NULLOBJECT, "商品规格不存在");
                }

                //这个逻辑好像是说如果goodssize库存相关数据为空，则复制goods里的数据?
                if (goodssize.getAllStockNum() == null) {
                    goodssize.setAllStockNum(goods.getAllStockNum());
                }
                if (goodssize.getRealStockCnt() == null) {
                    goodssize.setRealStockCnt(goods.getRealStockCnt());
                }
                if (goodssize.getRealSaleCnt() == null) {
                    goodssize.setRealSaleCnt(goods.getRealSaleCnt());
                }
                //因为放弃了用getGoodsMsg方法填充getAllStockNum值，这个判断应该也已经失败了，之后看有什么补救办法
//                if (goodssize.getAllStockNum()<=0) {
////                    goods.setStatus(2);
////                    goodsService.saveOrUpdate(goods);
//                    //因为当前事务必须失败回滚，因此考虑之后用异步任务来做这个状态改变
//                    throw new BusinessException(CommonInfoUtil.ISLOCK, "商品已售罄无法下单");
//                }

                //购物车信息，用于判断是否是购物车购买
                Goodsandgoodscar goodsandgoodscar = goodssizeAndCount.goodsandgoodscar;

                //获取当前下单实际数量,区分单个购买还是购物车购买
                int currentCount = (goodsandgoodscar == null) ? goodssizeAndCount.singleCount : goodsandgoodscar.getCount().intValue();

                //如果数量太大
                if (currentCount > goodssize.getRealStockNum()) {
                    throw new BusinessException(CommonInfoUtil.ISLOCK, "商品库存不足");
                }

                //获取图片
                HashMap<String, Object> prams = new HashMap<>();
                prams.put(Common.DELSTATUS, DelStatus.NODEL);
                prams.put("goodsId", goods.getId());
                prams.put(Common.TYPE, 1);
                Goodsimgs goodsimgs = goodsimgsService.getByParam(prams);
                if (goodsimgs == null && goodsimgs.getImgs().length() > 0) {
                    return null;
                }

                //保存订单，可以获取订单id,但目前金额是0
                userorderService.saveOrUpdate(userorder);

                //当前金额合计
                double currentAmount = (goodsandgoodscar != null) ? goodsandgoodscar.getAmount() : currentCount * goodssize.getPrice();

                //新建订单明细
                Ordergoods ordergoods = new Ordergoods(userorder.getId(), merchant.getId(), goods, goodsimgs.getImgs(),
                        goodssize, currentCount, currentAmount);
                ordergoodsService.saveOrUpdate(ordergoods);
                if (goodsandgoodscar != null) {
                    //订单生成后，删除购物车
                    goodsandgoodscar.setDelStatus(DelStatus.DEL);
                    if (goodscar != null) {
                        //并标记消减goodscar的金额
                        goodscar.setAmount(goodscar.getAmount() - currentAmount);
                    }
                    goodsandgoodscarService.saveOrUpdate(goodsandgoodscar);
                }

                //管理的数据：goodssize.realStockNum,goodssize.saleNum,goodssize.realSaleCnt, goods.realSaleCnt
                goodssize.setRealStockNum(goodssize.getRealStockNum() - currentCount);
                goodssize.setSaleNum(goodssize.getSaleNum() + currentCount);
                goodssize.setRealSaleCnt(goodssize.getRealSaleCnt() + currentCount);
//                goods.setRealSaleCnt(redisGoodssize.getRealSaleCnt()); 原本为这一行，但我认为应该累加
                goods.setRealSaleCnt(goods.getRealSaleCnt() + currentCount);
                //应该修改比如goods.realStockNum字段吗？
                goodssizeService.saveOrUpdate(goodssize);
                goodsService.saveOrUpdate(goods);
                //累加金额
                goodsAnt += currentAmount;
            }

            //一个店铺下所有明细生成完毕
            orderList.add(userorder);

            userorder.setAmount(goodsAnt);
            userorder.setPayableAmount(goodsAnt);
            if (remark != null && remark.length() > 0) {
                userorder.setRemark(remark);
            }
            userorderService.saveOrUpdate(userorder);

            //记录所有要支付的金额
            payAllAmount += goodsAnt;
        }

        if (goodscar != null) {
            //最后保存购物车（随着前面的循环内处理，amount已经被消除了）
            goodscarService.saveOrUpdate(goodscar);
        }

        //最后生成支付单据
        String serialNumber = ObjectUtil.getOrderNo(0, user.getId().toString());
        Yibaopayment yibaopayment = new Yibaopayment(user.getId(), serialNumber, payAllAmount);
        // add by winty 此字段以后废弃，改用将支付单流水号写入订单
        List<String> orderNoList = orderList.stream().map(o -> o.getOrderNo()).collect(Collectors.toList());
        String orderNos = ObjectUtil.listToString(orderNoList);
        yibaopayment.setOrderNos(orderNos);
        yibaopaymentService.saveOrUpdate(yibaopayment);

        // add by winty 支付单创建成功后,回写订单表里面的支付单流水号
        orderList.stream().forEach(order -> {
            Userorder updateUserOrder = new Userorder();
            updateUserOrder.setId(order.getId());
            updateUserOrder.setTopOrderNo(yibaopayment.getSerialNumber());
            userorderService.saveOrUpdate(updateUserOrder);
        });
        return yibaopayment.getSerialNumber();
    }

    /**
     * 取消订单/取消订单并退款的事务过程
     *
     * @param userOrder 要取消的userOrder
     * @param setRefund 取消订单的时候是否标记退款
     */
    @Transactional
    public void cancelUserOrder(Userorder userOrder, boolean setRefund) {
        //先锁userorder
        userOrder = userorderService.getForUpdate(userOrder.getId());

        //如果订单已关闭，不做任何操作返回（也不提示出错）
        if (userOrder.getStatus() == OrderStatusEnum.CLOSED.getKey()) {
            return;
        }
        //获取订单明细
        List<Ordergoods> orderGoodsList = ordergoodsService.getByParams(paramsNODel("orderId", userOrder.getId()));

        //不清楚要不要做订单明细为空的判断
//        if (ordergoodsList.isEmpty()) {
//            throw new BusinessException(CommonInfoUtil.DATAERROR, "订单没有明细");
//        }

        //采集所有的goodsId和goodssizeId
        HashSet<Long> allGoodsIdSet = new HashSet<>();
        HashSet<Long> allGoodssizeIdSet = new HashSet<>();
        //并且记录每一条ordergoodsId（订单明细）对应的goodssize的Id（货品规格ID）
        HashMap<Long, Long> ordergoodsId_to_goodssizeId = new HashMap<>();
        for (Ordergoods ordergoods : orderGoodsList) {
            HashMap<String, Object> prams = new HashMap<>();
            prams.put(Common.DELSTATUS, DelStatus.NODEL);
            prams.put("goodsId", ordergoods.getGoodsId());
            prams.put("specification", ordergoods.getSpecification());
            prams.put("price", ordergoods.getSlaePrice());
            Goodssize goodssize = goodssizeService.getByParam(prams);
            if (goodssize != null) {
                Goods goods = goodsService.get(goodssize.getGoodsId());
//                    goodsService.getGoodsMsg(goods, user);
                ordergoodsId_to_goodssizeId.put(ordergoods.getId(), goodssize.getId());
                if (goods != null) {
                    allGoodsIdSet.add(goods.getId());
                    allGoodssizeIdSet.add(goodssize.getId());
                }
            } //如果取不到规格就跳过，不清楚要不要做出错
        }
        //排序，锁定和加载
        LockedGoodsAndGoodssize lockedData =
                new LockedGoodsAndGoodssize(allGoodsIdSet, allGoodssizeIdSet);

        //锁定完成后逐个更新数量
        for (Ordergoods ordergoods : orderGoodsList) {
            Long goodssizeId = ordergoodsId_to_goodssizeId.get(ordergoods.getId());
            Goodssize goodssize = lockedData.goodssizeMap.get(goodssizeId);
            if (goodssize != null) {
                Goods goods = lockedData.goodsMap.get(goodssize.getGoodsId());
//                    goodsService.getGoodsMsg(goods, user);
                if (goods != null) {
                    //在锁定的数据里做变更
                    //管理的数据：goodssize.realStockNum,goodssize.saleNum,goods.realSaleCnt
                    if (goodssize.getRealStockNum() != null) {
                        goodssize.setRealStockNum(goodssize.getRealStockNum() + ordergoods.getCount());
                    }
                    if (goodssize.getSaleNum() != null) {
                        goodssize.setSaleNum(goodssize.getSaleNum() - ordergoods.getCount());
                    }
                    goods.setRealSaleCnt(goods.getRealSaleCnt() - ordergoods.getCount());
                    //保存锁定的数据
                    goodssizeService.saveOrUpdate(goodssize);
                    goodsService.saveOrUpdate(goods);
                }
            }
        }

        //最后修改订单状态
        userOrder.setStatus(-1);
        if (setRefund) {
            userOrder.setRefund(1);
        }
        userOrder.setGmtClose(new Date());
        userorderService.saveOrUpdate(userOrder);
    }


    /**
     * 取消订单的事务过程
     *  什么状态下可以取消订单
     *  取消订单包括未支付取消，已支付未发货取消，已支付已发货取消
     *  TODO
     *
     * @param userOrder 要取消的userOrder
     * @param type 1-未支付取消 2-已支付未发货取消 3-已支付已发货取消
     */
    @Transactional
    public void cancelUserOrder(Userorder userOrder, Integer type) {
        //先锁userorder
        userOrder = userorderService.getForUpdate(userOrder.getId());

        //如果订单已关闭，不做任何操作返回（也不提示出错）
        if (userOrder.getStatus() == OrderStatusEnum.CLOSED.getKey()) {
            return;
        }
        //获取订单明细
        List<Ordergoods> orderGoodsList = ordergoodsService.getByParams(paramsNODel("orderId", userOrder.getId()));

        //不清楚要不要做订单明细为空的判断
//        if (ordergoodsList.isEmpty()) {
//            throw new BusinessException(CommonInfoUtil.DATAERROR, "订单没有明细");
//        }

        //采集所有的goodsId和goodssizeId
        HashSet<Long> allGoodsIdSet = new HashSet<>();
        HashSet<Long> allGoodssizeIdSet = new HashSet<>();
        //并且记录每一条ordergoodsId（订单明细）对应的goodssize的Id（货品规格ID）
        HashMap<Long, Long> ordergoodsId_to_goodssizeId = new HashMap<>();
        for (Ordergoods ordergoods : orderGoodsList) {
            HashMap<String, Object> prams = new HashMap<>();
            prams.put(Common.DELSTATUS, DelStatus.NODEL);
            prams.put("goodsId", ordergoods.getGoodsId());
            prams.put("specification", ordergoods.getSpecification());
            prams.put("price", ordergoods.getSlaePrice());
            Goodssize goodssize = goodssizeService.getByParam(prams);
            if (goodssize != null) {
                Goods goods = goodsService.get(goodssize.getGoodsId());
//                    goodsService.getGoodsMsg(goods, user);
                ordergoodsId_to_goodssizeId.put(ordergoods.getId(), goodssize.getId());
                if (goods != null) {
                    allGoodsIdSet.add(goods.getId());
                    allGoodssizeIdSet.add(goodssize.getId());
                }
            } //如果取不到规格就跳过，不清楚要不要做出错
        }
        //排序，锁定和加载
        LockedGoodsAndGoodssize lockedData =
                new LockedGoodsAndGoodssize(allGoodsIdSet, allGoodssizeIdSet);

        //锁定完成后逐个更新数量
        for (Ordergoods ordergoods : orderGoodsList) {
            Long goodssizeId = ordergoodsId_to_goodssizeId.get(ordergoods.getId());
            Goodssize goodssize = lockedData.goodssizeMap.get(goodssizeId);
            if (goodssize != null) {
                Goods goods = lockedData.goodsMap.get(goodssize.getGoodsId());
//                    goodsService.getGoodsMsg(goods, user);
                if (goods != null) {
                    //在锁定的数据里做变更
                    //管理的数据：goodssize.realStockNum,goodssize.saleNum,goods.realSaleCnt
                    if (goodssize.getRealStockNum() != null) {
                        goodssize.setRealStockNum(goodssize.getRealStockNum() + ordergoods.getCount());
                    }
                    if (goodssize.getSaleNum() != null) {
                        goodssize.setSaleNum(goodssize.getSaleNum() - ordergoods.getCount());
                    }
                    goods.setRealSaleCnt(goods.getRealSaleCnt() - ordergoods.getCount());
                    //保存锁定的数据
                    goodssizeService.saveOrUpdate(goodssize);
                    goodsService.saveOrUpdate(goods);
                }
            }
        }
    }

}
